package com.lxh.lxhcrm.model.service;

import com.lxh.lxhcrm.model.dao.DepartmentDao;
import com.lxh.lxhcrm.model.dao.FunctionTyDao;
import com.lxh.lxhcrm.model.mappers.dept.DeptMapper;
import com.lxh.lxhcrm.model.mappers.emp.EmpMapper;
import com.lxh.lxhcrm.model.pojos.Department;
import com.lxh.lxhcrm.model.pojos.FunctionType;
import com.lxh.lxhcrm.model.pojos.Position;
import com.lxh.lxhcrm.model.pojos.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 开发团队 ：lxh
 * 开发人员 ：蒋鑫
 * 开发时间 ：2020/1/10 15：29
 * 类的名称 ：DeptService
 * 类的描述 ：权限模块service类，所有业务逻辑都在此
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DeptService {
    @Autowired
    DeptMapper deptMapper;
    @Autowired
    EmpMapper empMapper;
    @Autowired
    DepartmentDao departmentDao;
    @Autowired
    FunctionTyDao functionTyDao;
    /*查询所有部门*/
    public List<Department> findAllDept(){
        return deptMapper.findAllDept();
    };
    /*查询所点击部门的权限组的id集合*/
    public List<Integer> findFunType(Integer departmentId){
        return deptMapper.findFunType(departmentId);
    };
    /*点击查询部门的角色*/
    public List<Role> findRole(int departmentId){
        return deptMapper.findRole(departmentId);
    }
    /*查询正常的所有部门*/
    public List<Department> findDept(){
        return deptMapper.findDept();
    };
    /*根据部门id 查询部门下的职位*/
    public List<Position> findPost(Integer departmentId){
        return deptMapper.findPost(departmentId);
    };

    /**
     * 查询所有部门及其下面得员工
     * @author：鞠玮
     * @return List<Department>:部门对象
     */
    public List<Department> findAllDeptAndEmp(){
        return empMapper.findAllDeptAndEmp();
    }
/* 查询所有的权限组*/
    public List<FunctionType> allFunctionType(){
        return deptMapper.allFunctionType();
    }
/*修改权限组*/
    public void savePrivilege(Integer deptId,List<Integer> funTypeId){
        //操作部门 功能组 的中间表
        Department department = departmentDao.findById(deptId).get();
        System.out.println("权限组开始操作");
        //获取中间表的集合
        List<FunctionType> funType = department.getFunType();
        if(funType == null){
            funType = new ArrayList<FunctionType>();
        }
        funType.clear();//先清空
        //给集合添加数据，给表新增记录
        funType.addAll((Collection)functionTyDao.findAllById(funTypeId));
        //重置集合，相当于执行中间表新增
        if (funType.size()==0){
        }else{
            department.setFunType(funType);
        }
    }

    /*根据权限组id  查询权限组*/
    public List<FunctionType> findFtName(String functionTypeId){
        return deptMapper.findFtName(functionTypeId);
    }

}
